use "${data}BugandaAnalysisReplication.dta", clear

* setting up
set scheme cleanplots, perm
drop if year == 2000 | mailo_census == .

gen moremailo = mailo_census > 0.09 & mailo_census ~=.
gen morecustom = customary_census > 0.69 & customary_census ~=.
gen morefree = moremailo == 0 & morecustom == 0
gen cat = 1 if moremailo == 1
replace cat = 2 if morecustom == 1
replace cat = 3 if morefree == 1
label define cats 1 "Mailo" 2 "Customary"  3 "Private"
la values cat cats
la var cat "Land tenure type"

la var forestloss_han50 "Forest loss (0/1), GFC Data"

* Deforestation summary graphs *
foreach v in 50 30 10  {

preserve
keep if gte`v' == 1
bysort cat: egen tot = count(cat)
egen total = count(cat)
replace tot = tot/total

twoway (lpoly forestloss_han`v' year if moremailo == 1) (lpoly forestloss_han`v' year if morecustom == 1, lcolor(gray) lpattern (shortdash)) ///
(lpoly forestloss_han`v' year if morefree == 1, lcolor(navy) ), ///
 legend(label(1 "Mailo") label(2 "Customary") label(3 "Private") pos(3)) ///
 xtitle(Year) ytitle(Probability deforestation) saving("${figures}temp1`v'", replace)

graph bar tot, over(cat, label) fysize(20) ytitle("Proportion of sample") saving("${figures}temp2`v'", replace)
gr combine "${figures}temp1`v'" "${figures}temp2`v'", col(1)
gr export "${figures}PolyTimeBuganda`v'.eps", replace


graph bar forestloss_han`v', label over(year, label(angle(90))) over(cat)
gr export "${figures}BarTimeBuganda`v'.eps", replace

restore
}
replace forestloss_han50 = . if gte50 == 0
replace forestloss_han30 = . if gte30 == 0
replace forestloss_han10 = . if gte10 == 0


************ Summary stats by land type ******************
estpost sum  forestloss_han50 forestloss_han30 forestloss_han10 ruggedness maize banana sorghum tea pop_2010 dist_dirtr dist_impro elevation slope_perc if buganda == 1
estimates store bug
estpost sum  forestloss_han50 forestloss_han30 forestloss_han10 ruggedness maize banana sorghum tea pop_2010 dist_dirtr dist_impro elevation slope_perc if borderbuganda == 1
estimates store bord
estpost sum  forestloss_han50 forestloss_han30 forestloss_han10 ruggedness maize banana sorghum tea pop_2010 dist_dirtr dist_impro elevation slope_perc if borderbuganda == 0 & buganda == 0
estimates store other

esttab bug bord other  using "${tables}\sumstatsbasic.tex", prehead("& Buganda & Bordering & Rest of country \\ \hline \\") replace fragment cells("mean(fmt(2))") label style (tex) collabels(none) nomtitle noline nonumbers

eststo clear 

************ Balance of covarates by % mailo in 2002 ******************
// standardizing coefficients to interpret as beta coefficients
preserve

egen sd_mailo = std(mailo)
label var sd_mail "\% Mailo"

* Whole country *
foreach v in treecover2000 ruggedness maize banana sorghum tea pop_2010 dist_dirtr dist_impro elevation slope_perc protect {
    egen sd_`v' = std(`v')
    eststo: reghdfe sd_`v'  sd_mailo if year==2001, abs(region) cluster(district)
}
esttab using "${tables}\bal_cov.tex", se b(%12.2f) se(%12.2f) nonotes style(tex) label replace nomtitles fragment nonumbers starlevels(* 0.10 ** 0.05 *** 0.01) keep(sd_mailo) prehead(\begin{tabular}{l*{12}{c}} \hline \hline  & Forest & Ruggedness & Maize & Banana & Sorghum & Tea & Population & Dirt Road & Improved Road & Elevation & Slope & Protected  \\ & (2000) & Index & Suitability & Suitability & Suitability & Suitability & (2010) & (Km to) & (Km to) & & Percentage & Area  \\ & (1) & (2) & (3) & (4) & (5) & (6) & (7) & (8) & (9) & (10) & (11) & (12) \\ \hline) posthead(\\) prefoot(\\) postfoot(\hline \hline \end{tabular} \begin{tablenotes} \item \scriptsize{* p$<$ 0.10, ** p$<$0.05, *** p $<$ 0.01. All variables standardized to mean zero, standard deviation one. Unit of observation is a grid cell. Percent mailo land is the percent of land held in mailo tenure in 2002 in the county in which the grid cell is located. All estimates control for region fixed effects and cluster standard errors at the district level.} \end{tablenotes} )
estimates clear

restore




* Summary stats table: Land tenure by Region *
******************************************************************************
use "${data}tenure_census.dta", clear 

label var mailo "Mailo"
label var freehold "Freehold"
label var leasehold "Leasehold"
label var custom "Customary"

estpost sum mailo freehold leasehold custom if region=="Central"
estimates store central
esttab central using "${tables}/tenure02_sumstat.tex", label style(tex) nonotes nomtitles noobs fragment nonumber cells("count(fmt(0) label(N)) mean(fmt(2) label(Mean)) sd(fmt(2) label(SD)) min(fmt(2) label(Min)) max(fmt(2) label(Max))") replace prehead(\begin{tabular}{l*{5}{c}} \hline \hline) posthead(\hline \multicolumn{6}{l}{\textit{Central}}\\) 

estpost sum mailo freehold leasehold custom if region=="Eastern"
estimates store east
esttab east using "${tables}/tenure02_sumstat.tex", label style(tex) nonotes nomtitles noobs fragment nonumber cells("count(fmt(0) label()) mean(fmt(2) label()) sd(fmt(2) label()) min(fmt(2) label()) max(fmt(2) label())") append posthead(\\ \multicolumn{6}{l}{\textit{Eastern}}\\) 

estpost sum mailo freehold leasehold custom if region=="Northern"
estimates store north
esttab north using "${tables}/tenure02_sumstat.tex", label style(tex) nonotes nomtitles noobs fragment nonumber cells("count(fmt(0) label()) mean(fmt(2) label()) sd(fmt(2) label()) min(fmt(2) label()) max(fmt(2) label())") append posthead(\\ \multicolumn{6}{l}{\textit{Northern}}\\) 

estpost sum mailo freehold leasehold custom if region=="Western"
estimates store west
esttab west using "${tables}/tenure02_sumstat.tex", label style(tex) nonotes nomtitles noobs fragment nonumber cells("count(fmt(0) label()) mean(fmt(2) label()) sd(fmt(2) label()) min(fmt(2) label()) max(fmt(2) label())") append posthead(\\ \multicolumn{6}{l}{\textit{Western}}\\) postfoot(\hline \hline \end{tabular})

estimates clear

